export default class StepCompute{
    //移动延迟时间
    private delay:number = 0;
    private startDelay:number = 0;
    private maxDelay:number = 0;
    private minDelay:number = 0;
    private time:number = 0;
    public step:number = 0;
    private a:number = 0;
    //-1减速 0匀速 1加速
    private status:number = 0;
    private config:IStepComputeData = {
        startDelay:700,
        maxDelay:600,
        minDelay:50,
        a:250,
        step:1,
        uniformTime:500
    };
    private uniformTime:number = 0;
    private updateCallback:(() => void) | undefined;
    constructor(config?:IStepComputeData){
        if(config){
            this.config = config;
        }
        this.reset();
    }
    public reset(){
        if(this.config.startDelay !== undefined){
            this.delay = this.config.startDelay;
        }
        if(this.config.maxDelay !== undefined){
            this.maxDelay = this.config.maxDelay;
        }
        if(this.config.startDelay !== undefined){
            this.startDelay = this.config.startDelay;
        }
        if(this.config.minDelay !== undefined){
            this.minDelay = this.config.minDelay;
        }
        if(this.config.a !== undefined){
            this.a = this.config.a;
        }
        if(this.config.step !== undefined){
            this.step = this.config.step;
        }
        if(this.config.uniformTime !== undefined){
            this.uniformTime = this.config.uniformTime;
        }
        this.time = this.delay;
        this.status = 1;
        this.speedUp();
    }
    public update(){
        if(this.time <= 0){
            this.updateCallback && this.updateCallback();
        } else {
            this.step = 0;
        }
        this.time -= 16;
        
        if(this.status == 0){
            this.uniformTime -= 16;
            if(this.uniformTime <= 0){
                this.speedDown();
            }
        }
    }
    //加速
    public speedUp(){
        this.delay = this.startDelay;
        this.status = 1;
        this.a = -(this.config.a || 0 );
        this.updateCallback = ()=>{
            this.delay += this.a;
            if(this.delay <= this.minDelay){
                this.speedUniform();
            }
            this.time = this.delay;
            this.step = 1;
        }
    }
    //减速
    public speedDown(){
        this.delay = this.minDelay;
        this.status = -1;
        this.a = this.config.a || 0;

        this.updateCallback = ()=>{
            this.delay += this.a;
            if(this.delay >= this.maxDelay){
                this.delay = this.maxDelay;
            }
            this.time = this.delay;
            this.step = 1;
        }
    }
    public speedUniform(){
        this.delay = this.minDelay;
        this.status = 0;
        this.a = 0;
        this.updateCallback = ()=>{
            this.time = this.delay;
            this.step = 1;
        }
    }
}